package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.OpenBuilder;
import com.zaofan.cache.CacheManage;
import com.zaofan.service.OpenBuilderService;
import com.zaofan.utils.JsonUtils;

@WebServlet("/mobile/builder")
public class BuilderController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private OpenBuilderService builderService = new OpenBuilderService();
	/**
	 * 城市 开通区域 列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String temp = request.getParameter("areaId");
			if (temp == null) {
				JsonUtils.writeListFailJson("缺少区域信息", response);
				return;
			}
			int areaId = Integer.valueOf(temp);
			if (CacheManage.openBuilders.containsKey(areaId)) {
				JsonUtils.writeSuccessJson("查询成功", CacheManage.openBuilders.get(areaId), response);
			} else {
				JsonUtils.writeSuccessJson("查询成功", Collections.EMPTY_LIST, response);
			}
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}
	
	/**
	 * 城市 开通区域 查找 列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String key = request.getParameter("key");
			String cityId = request.getParameter("cityId");
			if(key == null){
				JsonUtils.writeListFailJson("请输入需要查找的信息", response);
				return;
			}
			List<OpenBuilder> builders = builderService.findByNameLike(key, cityId);
				JsonUtils.writeSuccessJson("查询成功", builders, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}
	
}
